package com.atguigu.day05;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class Flink08_TimeWindow_Tumbling_AggFun {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);


        //2.从端口获取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数转为Tuple2元组
        SingleOutputStreamOperator<Tuple2<String, Long>> wordToOneDStream = streamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
                    out.collect(Tuple2.of(value, 1L));

            }
        });

        //4.对相同单词的数据进行聚合
        KeyedStream<Tuple2<String, Long>, Tuple> keyedStream = wordToOneDStream.keyBy(0);

        // 5.开启一个基于时间的滚动窗口
        WindowedStream<Tuple2<String, Long>, Tuple, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

        //TODO 使用增量聚合函数AggFun计算累加结果
        window.aggregate(new AggregateFunction<Tuple2<String, Long>, Long, Long>() {
            /**
             * 初始化累加器
             *
             * @return
             */
            @Override
            public Long createAccumulator() {
                System.out.println("初始化累加器");
                return 0L;
            }

            /**
             * 累加操作
             *
             * @param value
             * @param accumulator
             * @return
             */
            @Override
            public Long add(Tuple2<String, Long> value, Long accumulator) {
                System.out.println("累加操作");
                return value.f1 + accumulator;
            }

            /**
             * 获取最终的结果（累加器中的值）
             *
             * @param accumulator
             * @return
             */
            @Override
            public Long getResult(Long accumulator) {
                System.out.println("获取结果");
                return accumulator;
            }

            /**
             * 合并累加器
             * 这个方法只在会话窗口中合并窗口时调用
             *
             * @param a
             * @param b
             * @return
             */
            @Override
            public Long merge(Long a, Long b) {
                System.out.println("合并");
                return a + b;
            }
        }).print();

        env.execute();


    }
}
